<?php

namespace App\Support;


class WechatLogin
{
    var $appid = "";
    var $appsecret = "";
    var $url = "";
    //构造函数，获取Access Token
    public function __construct($appid = NULL, $appsecret = NULL)
    {
        if($appid){
            $this->appid = $appid;
        }
        if($appsecret){
            $this->appsecret = $appsecret;
        }
    }

    public function getCode($redirectUrl)
    {
        $url = $this->createOauthUrlForCode($redirectUrl);
        Header("Location: $url");
    }
    //获取用户基本信息
    public function get_user_info($code)
    {
        $authInfo = $this->getAuthInfo($code);
        $url = "https://api.weixin.qq.com/sns/userinfo?access_token=".$authInfo['access_token']."&openid=".$authInfo['openid']."&lang=zh_CN";
        $res = $this->https_request($url);
        return json_decode($res, true);
    }

    public function getAuthInfo($code)
    {
        $token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$this->appid.'&secret='.$this->appsecret.'&code='.$code.'&grant_type=authorization_code';
        $res = $this->https_request($token_url);
        return json_decode($res, true);
    }

//https请求
    public function https_request($url, $data = null)
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        if (!empty($data)){
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        curl_close($curl);
        return $output;
    }


    /*
	 * 	作用：生成可以获得code的url
	 */
    public function  createOauthUrlForCode($redirectUrl){
        $urlObj["appid"] = $this->appid;
        $urlObj["redirect_uri"] = urlencode($redirectUrl);
        $urlObj["response_type"] = "code";
        $urlObj["scope"] = "snsapi_userinfo";
        $urlObj["state"] = "STATE"."#wechat_redirect";
        $bizString = $this->formatBizQueryParaMap($urlObj, false);
        return "https://open.weixin.qq.com/connect/oauth2/authorize?".$bizString;
    }

    /*
     * 	作用：格式化参数，签名过程需要使用
     */
    public function formatBizQueryParaMap($paraMap, $urlencode){
        $buff = "";
        ksort($paraMap);
        foreach ($paraMap as $k => $v){
            if($urlencode){
                $v = urlencode($v);
            }
            $buff .= $k . "=" . $v . "&";
        }
        $reqPar = '';
        if (strlen($buff) > 0){
            $reqPar = substr($buff, 0, strlen($buff)-1);
        }
        return $reqPar;
    }

    /**
     * 	作用：生成可以获得openid的url
     */
    public function createOauthUrlForOpenid($code){
        $urlObj["appid"] = $this->appid;
        $urlObj["secret"] = $this->appsecret;
        $urlObj["code"] = $code;
        $urlObj["grant_type"] = "authorization_code";
        $bizString = $this->formatBizQueryParaMap($urlObj, false);
        return "https://api.weixin.qq.com/sns/oauth2/access_token?".$bizString;
    }

    /**
     * 	作用：通过curl向微信提交code，以获取openid
     */
    public function getOpenid($code){
        $url = $this->createOauthUrlForOpenid($code);
        $res = $this->https_request($url);
        $data =  json_decode($res, true);
        return $data['openid'];
    }

}